package popple.display.text
{

	import com.popple.d3dfiles.animation.data.Bone;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display3D.Context3DTextureFormat;
	import flash.display3D.textures.Texture;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import popple.core.PP2D;
	import popple.core.RenderHelper;
	import popple.data.UVHelp;
	import popple.display.Image;
	import popple.display.TBitmap;
	import popple.math.getDownPower2;
	import popple.math.getUpPower2;
	
	
	
	public class TextField extends Image
	{
		private var _text:String;
		private var _textField:flash.text.TextField;
		private var _textFormat:TextFormat;
		private var _autoSize:String;
		private var _wordWarp:Boolean;
		private var _bd:BitmapData;
		private var _texture:Texture;
		private var _bitmapBuffer:BitmapData;
		private var _uvBuffer:Vector.<Number>
		private var _uvData:UVHelp=new UVHelp;
		private var _textChange:Boolean=false;
		
		private var _change:Boolean=false;
		
		private var _bufferWidth:Number;
		private var _bufferHeight:Number;
		
		private var _bmpData:TBitmap;
		
		private var _drawed:Boolean;
		public function TextField(text:String="")
		{
			super();
			_bmpData=new TBitmap;
			_textField=new flash.text.TextField;
			_textField.text=text;
			
			
		}
		public function set text(value:String):void
		{
			
			_textField.text=value;	
			_drawed=false;
		}
		
		public function setTextFormat(format:TextFormat):void
		{

			_textField.setTextFormat(format);
			_drawed=false;
		}
		public function set multiline(value:Boolean):void
		{
			_textField.multiline=value;
			_drawed=false;
		}
		public function set autoSize(value:String):void
		{
	
			_textField.autoSize=value;
			_drawed=false;
		}
		override public function set width(value:Number):void
		{
			_textField.width=value;
			_drawed=false;
		}
		override public function set height(value:Number):void
		{
			_textField.height=value;
			_drawed=false;	
		}
		public function set wordWarp(value:Boolean):void
		{
		
			_textField.wordWrap=value;
			_drawed=false;
			
		}
		override protected function preRender():void
		{
			
			if(_drawed==false)
			{
				drawTextField();
				_drawed=true;
			}
			
		}
		private function drawTextField():void
		{
			
			var width:Number=_textField.width;
			var height:Number=_textField.height;
			var maxWidth:uint=getUpPower2(int(width));
			var maxHeight:uint=getUpPower2(int(height));
			
			if(maxWidth!=_bufferWidth||maxHeight!=_bufferHeight)
			{
				_bufferWidth=maxWidth;
				_bufferHeight=maxHeight;
				if(_bitmapBuffer!=null)_bitmapBuffer.dispose();
				_bitmapBuffer=new BitmapData(maxWidth,maxHeight,true,0xff0000);
			}

			_bitmapBuffer.draw(_textField);
			
			_bmpData.setBitmap(_bitmapBuffer,new Rectangle(0,0,width,height));
			this.bitmap=_bmpData;
		}
		
	}
}